    //
//  SearchVC_iPad.m
//  WRY
//
//  Created by zhong sckj on 10-11-5.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SearchVC_iPad.h"
#import "AppDelegate_iPad.h"
#import "CommonJson.h"
#import "WebPath.h"
#import "SearchListCell.h"

#define LEFTVIEWHEIGHT 644
@implementation SearchVC_iPad
@synthesize tblView,dataArray,keyword;
@synthesize _cell;
@synthesize rootVc;
@synthesize pageCell;

- (void)requestForRegion:(NSString *)body methodName:(NSString *)mthod {
	[self showLoading:0 centerY:0];
	NSString *bodyStr = [NSString stringWithFormat:@"LoginGuid=%@&%@",[[AppDelegate_iPad currentDelegate] GUID],body];
	//bodyStr = [bodyStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	[[CommonJson sharedInstance] requestToPath:[WebPath enterprisePath]
									methodName:mthod 
									bodyString:bodyStr 
									 targetObj:self
							  targetMethodName:@"eventForRequest:"
									notifiName:@"Page_enterList"];
	
}
- (void)eventForRequest:(NSNotification *)notification {
	[self hideLoading];
	[[CommonJson sharedInstance] removeTarget:self notiName:[notification name]];
	NSObject *object  = [notification object];
	if ([object isKindOfClass:[NSDictionary class]]) {
		NSDictionary *dict = (NSDictionary *)object;
		if ([[dict objectForKey:@"Status"] intValue]) 
		{
			if ([[dict objectForKey:@"Data"] isKindOfClass:[NSDictionary class]])
			{
				
				NSDictionary  *enterlist=(NSDictionary *)[dict objectForKey:@"Data"];
				NSArray *array = [enterlist objectForKey:@"EntityList"];
				[dataArray addObjectsFromArray:array];
					
				NSString *count =[enterlist objectForKey:@"RecordCount"];
				enlistTotalCount = [count intValue];
				//pageCount=enlistTotalCount%pageSize==0?enlistTotalCount/pageSize:(enlistTotalCount/pageSize)+1;
				if (enlistTotalCount > [dataArray count])
				{
					[pageButton setHidden:NO];
					[pageButton setTitle:@"显示更多" forState:UIControlStateNormal];
				}
                else {
                    [pageButton setHidden:YES];

                }
                if([array count]<1){
                     [pageButton setHidden:YES];
                }
				
				[tblView reloadData];
				
			}
			else {
				////[self showConnectionError];
			}
		}
		else {
			////[self showConnectionError];
		}
		
	}
	else {
		////[self showConnectionError];
	}	
}
-(void)linkData
{
	NSString *body = [NSString stringWithFormat:@"PageSize=%@&PageNow=%@&CantonCodes=%@&EnterpriseName=%@&EnterpriseTypeCodes=%@&DetailTypeCode=%@",
					  [NSString stringWithFormat:@"%ld",(long)pageSize],[NSString stringWithFormat:@"%d",pagecurrentCount],[[AppDelegate_iPad currentDelegate] userCantonCodes],keyword,@"",@""];
	NSString *methodname=@"Page__EnterpriseList";
	
	[self requestForRegion:body methodName:methodname];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	dataArray=[[NSMutableArray alloc]init];
	CGRect rect=CGRectMake(714, 100,300, 644);
	self.view.frame=rect;
	[self createTableView];
	enlistTotalCount=0;
	pagecurrentCount=1;
	pageSize=20;
	
	//[self linkData];
}
//-(IBAction)prePageAction
//{
//	if (pagecurrentCount>1) {
//		pagecurrentCount--;
//		[self linkData];
//	}
//	
//}
//-(IBAction)nextPageAction
//{
//	if (pagecurrentCount<pageCount) {
//		pagecurrentCount++;
//		[self linkData];
//	}
//	
//}
-(IBAction)morePageCount
{
	pagecurrentCount++;
	[self linkData];
}

-(void)createTableView
{
	UIImageView *borderView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, LEFTVIEWHEIGHT)];
	[borderView setImage:[UIImage imageNamed:@"sidebar285x71_1.png"]];
	borderView.userInteractionEnabled = YES;
	
	tblView=[[UITableView alloc]initWithFrame:CGRectMake(1.0,0,borderView.frame.size.width-2,LEFTVIEWHEIGHT)];
	tblView.delegate=self;
	tblView.dataSource=self;
	[borderView addSubview:tblView];
	[self.view addSubview:borderView];
	[borderView release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	if (enlistTotalCount>[dataArray count] && [dataArray count]>0) {
		return 2;
	}
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
	if (section==1) {
		return 1;
	}
	else
	{
		return [dataArray count]>0?[dataArray count]:1;
	}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (indexPath.section==1) {
		return pageCell;
	}
	else {
		static NSString *searchdentifier_ipad = @"searchdentifier_ipad";
		SearchListCell *cell=(SearchListCell *)[tblView dequeueReusableCellWithIdentifier:searchdentifier_ipad];
		if (cell==nil) {
			[[NSBundle mainBundle]loadNibNamed:@"SearchListCell" owner:self options:nil];
			cell=_cell;
			cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
		}
		if ([dataArray count]>0)
		{
			NSDictionary *dict = [dataArray objectAtIndex:indexPath.row];
			cell.nameLabel.text = [dict objectForKey:@"EnterpriseName"];
			cell.nameLabel.minimumFontSize=0.0;
			cell.nameLabel.adjustsFontSizeToFitWidth=YES;
            
            NSString *enterpriseType=[NSString stringWithFormat:@"%@",[dict objectForKey:@"EnterpriseType"]];
            enterpriseType=[NSString stringWithFormat:@"0%@",enterpriseType];
			if ([enterpriseType isEqualToString:@"01"]) {
				cell.enterImageView.image=[UIImage imageNamed:@"entypewater2.png"];
			}
			else if([enterpriseType isEqualToString:@"02"])
			{
				cell.enterImageView.image=[UIImage imageNamed:@"entypeair2.png"];
			}
			else if([enterpriseType isEqualToString:@"03"])
			{
				cell.enterImageView.image=[UIImage imageNamed:@"entypesewage.png"];
			}
			//cell.enterImageView.image=[UIImage imageNamed:@"image78.png"];
		}
        else 
		{
			cell.accessoryType=UITableViewCellAccessoryNone;
			cell.selectionStyle=UITableViewCellSelectionStyleNone;
			cell.nameLabel.text = @"没有此条件的搜索记录!";
			
		}
		return cell;
	}

	
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if ([dataArray count]>0) {
		NSDictionary *dict = [dataArray objectAtIndex:indexPath.row];
		//005561
		NSString *comaycode=[dict objectForKey:@"EnterpriseCode"];
        NSString *comayTypeCode=[dict objectForKey:@"EnterpriseType"];
		
        [rootVc addcompanydeatilVC:comaycode companyTypeCode:comayTypeCode 
                          siteCode:@""
                      sitetypeCode:@""
                       factoryCode:@""];
        
        
		[rootVc release];
	}
	
	
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[pageCell release];
	[rootVc release];
	[_cell release];
	[keyword release];
	[dataArray release];
	[tblView release];
    [super dealloc];
}


@end
